//
//  HMStatusCell.swift
//  MyWeibo
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit


// 头像的大小
let HMStatusCellHeadImgWH: CGFloat = 50

// 昵称的字体大小
let HMStatusCellNameFontSize: CGFloat = 14
// 时间与来源
let HMStatusCellTimeFontSize: CGFloat = 10

// cell里面公共的间距
let HMStatusCellMargin: CGFloat = 10

// 内容label
let HMStatusCellContentFontSize: CGFloat = 14

// 底部视图的高度
let HMStatusCellBottomViewH: CGFloat = 35


class HMStatusCell: UITableViewCell {
    
    // 取到toolBar 的顶部约束
    var toolBarTopCons: Constraint?
    
    //创建一个模型传递数据
    var statusViewModel:HMStatusViewModel?{
        
        //在模型数据加载完毕后赋值
        didSet{
            originalView.statusViewModel = statusViewModel
//            retweetView.statusViewModel = statusViewModel
            
//             1. 取到 toolBar 的 top 约束, 2. 再让其失效
            toolBarTopCons?.uninstall()
//            判断是否有转发数据 如果有就转发
            if statusViewModel?.status?.retweeted_status != nil {
            
                //转发微博 设置数据
                retweetView.statusViewModel = statusViewModel
                //显示转发视图
                retweetView.hidden = false
//                更改约束toolbar的顶部为转发微博的底部
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
            else {
                
                //如果没有转发隐藏转发视图
                retweetView.hidden = true
                //toolbar的约束改为原创微博的bttom
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    toolBarTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
            }
        
        }
        
    }
    
    //修改初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    //系统生成和初始化相配应的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //记载cell 中的控件
    func setupUI(){
        
        //设置当前cell选中的样式
        selectionStyle = .None
        
        
        //设置当前containtview的背景
//       self.contentView.backgroundColor = UIColor(white: 180/255, alpha: 1)
        //        backgroundColor = UIColor(red: CGFloat(random()%256)/255, green: CGFloat(random()%256)/255, blue: CGFloat(random()%256)/255, alpha: 1)
        
        
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        //设置约束
        
        // 原创微博添加约束
        originalView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentView).offset(HMStatusCellMargin)
            make.top.leading.trailing.equalTo(contentView)
        }
        
        //转发微博约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)

        }
        
        //底部按钮约束
        toolBar.snp_makeConstraints { (make) -> Void in
            toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(originalView)
            make.height.equalTo(HMStatusCellBottomViewH)

        }
        
       //cell自身约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_bottom)
            make.leading.top.trailing.equalTo(self)
            
        }
    }
    
    
    //懒加载控件
    private lazy var originalView:HMOriginalStatusView = HMOriginalStatusView()
    //懒加载底部view
    private lazy var toolBar:HMStatusToolBar = HMStatusToolBar()
    //懒加载转发微博
    private lazy var retweetView: HMRetweetStatusView = HMRetweetStatusView()
}
